package com.tyqx.hashchainbiz.controller.jzt;

import com.tyqx.hashchainbean.module.HashChainStatistics;
import com.tyqx.hashchainbean.vo.*;
import com.tyqx.hashchainbiz.service.HashChainStatisticsService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/JZTApi/count")
@CrossOrigin
public class JZTCountController {

    @Resource
    private HashChainStatisticsService hashChainStatisticsService;

    @RequestMapping(value = "/selectCount", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectCount() throws Exception {
        double logisticsFileSizeCount = 0.00;
        Integer logisticsInfoCount = 0;
        double codeFileSizeCount = 0.00;
        Integer codeInfoCount = 0;
        double GS1FileSizeCount = 0.00;
        Integer GS1InfoCount = 0;
        List<HashChainStatistics> hashChainStatisticsList=new ArrayList<>();
            try {
                hashChainStatisticsList = hashChainStatisticsService.selectCountGroupByDataType();
            }catch (Exception e){
                CountVO countVO = new CountVO();
                countVO.setJztLogisticsCount(logisticsInfoCount);
                countVO.setJztAttachmentBusinessFileSizeCount(logisticsFileSizeCount);
                countVO.setJztCodeCirculationListCount(codeInfoCount);
                countVO.setJztCodeCirculationAttachmentFileSizeCount(codeFileSizeCount);
                countVO.setJztGS1Count(GS1InfoCount);
                countVO.setJztGS1AttachmentFileSizeCount(GS1FileSizeCount);
                String message = "jztLogisticsList info count:" + logisticsInfoCount + ",jztAttachmentBusiness file size count:" + logisticsFileSizeCount + "MB     "+
                        "jztCodeCirculationList info count:" + codeInfoCount + ",jztCodeCirculationAttachment file size count:" + codeFileSizeCount + "MB      "+
                        "jztGS1List info count:" + GS1InfoCount + ",jztGS1Attachment file size count:" + GS1FileSizeCount + "MB";;
                return DtoUtil.returnFail(message,"100002", countVO);
            }
            String dataTypeAttachment = "jztAttachmentBusiness";

            String dataTypeLogisticsList = "jztLogisticsList";

            String dataTypeJztCodeCirculationAttachment="jztCodeCirculationAttachment";

            String dataTypeJztCodeCirculation="jztCodeCirculationList";

            String dataTypeJztGS1List="jztGS1List";

            String dataTypeJztGS1Attachment="jztGS1Attachment";

            for (int i = 0; i < hashChainStatisticsList.size(); i++) {
                HashChainStatistics hashChainStatistics = hashChainStatisticsList.get(i);
                String dataType = hashChainStatistics.getDataType();
                if (dataTypeAttachment.equals(dataType)) {
                    logisticsFileSizeCount = hashChainStatistics.getDataCount();
                } else if (dataTypeLogisticsList.equals(dataType)) {
                    logisticsInfoCount = (int) hashChainStatistics.getDataCount();
                } else if(dataTypeJztCodeCirculation.equals(dataType)){
                    codeInfoCount=(int) hashChainStatistics.getDataCount();
                }else if(dataTypeJztCodeCirculationAttachment.equals(dataType)){
                    codeFileSizeCount=hashChainStatistics.getDataCount();
                }else if(dataTypeJztGS1List.equals(dataType)){
                    GS1InfoCount=(int) hashChainStatistics.getDataCount();
                }else if(dataTypeJztGS1Attachment.equals(dataType)){
                    GS1FileSizeCount=hashChainStatistics.getDataCount();
                }
            }
            if (hashChainStatisticsList.size() > 0) {
                CountVO countVO = new CountVO();
                countVO.setJztLogisticsCount(logisticsInfoCount);
                countVO.setJztAttachmentBusinessFileSizeCount(logisticsFileSizeCount);
                countVO.setJztCodeCirculationListCount(codeInfoCount);
                countVO.setJztCodeCirculationAttachmentFileSizeCount(codeFileSizeCount);
                countVO.setJztGS1Count(GS1InfoCount);
                countVO.setJztGS1AttachmentFileSizeCount(GS1FileSizeCount);
                String message = "jztLogisticsList info count:" + logisticsInfoCount + ",jztAttachmentBusiness file size count:" + logisticsFileSizeCount + "MB\n"+
                        "jztCodeCirculationList info count:" + codeInfoCount + ",jztCodeCirculationAttachment file size count:" + codeFileSizeCount + "MB\n"+
                        "jztGS1List info count:" + GS1InfoCount + ",jztGS1Attachment file size count:" + GS1FileSizeCount + "MB\n";
                return DtoUtil.returnSuccess(message, countVO);
            } else {
                return DtoUtil.returnFail("No count", "100001");
            }
    }


    @RequestMapping(value = "/selectJztLogisticsCountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectJztLogisticsCountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataTypeLogisticsList="jztLogisticsList";
        Integer count=0;
        try {
            count=(int)hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataTypeLogisticsList);
        }catch (Exception e){
            JztLogisticsCountVO jztLogisticsCountVO=new JztLogisticsCountVO();
            jztLogisticsCountVO.setJztLogisticsCount(count);
            return  DtoUtil.returnFail("jztLogistics count:"+count,"100002",jztLogisticsCountVO);
        }

        if(count>0){
            JztLogisticsCountVO jztLogisticsCountVO=new JztLogisticsCountVO();
            jztLogisticsCountVO.setJztLogisticsCount(count);
            return  DtoUtil.returnSuccess("jztLogistics count:"+count,jztLogisticsCountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }
    }

    @RequestMapping(value = "/selectJztAttachmentBusinessCountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectJztAttachmentBusinessCountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataTypeAttachment="jztAttachmentBusiness";
        double count=0.00;
        try {
            count=hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataTypeAttachment);
        }catch (Exception e){
            JztAttachmentBusinessFileSizeCountVO jztAttachmentBusinessFileSizeCountVO=new JztAttachmentBusinessFileSizeCountVO();
            jztAttachmentBusinessFileSizeCountVO.setJztAttachmentBusinessFileSizeCount(count);
            return  DtoUtil.returnFail("jztAttachmentBusiness file size count:"+count+"MB","100002",jztAttachmentBusinessFileSizeCountVO);
        }

        if(count>0){
            JztAttachmentBusinessFileSizeCountVO jztAttachmentBusinessFileSizeCountVO=new JztAttachmentBusinessFileSizeCountVO();
            jztAttachmentBusinessFileSizeCountVO.setJztAttachmentBusinessFileSizeCount(count);
            return  DtoUtil.returnSuccess("jztAttachmentBusiness file size count:"+count+"MB",jztAttachmentBusinessFileSizeCountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }


    }


    @RequestMapping(value = "/selectJztCodeCirculationCountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectJztCodeCirculationCountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataType="jztCodeCirculationList";
        Integer count=0;
        try {
            count=(int)hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataType);
        }catch (Exception e){
            JztCodeCirculationListCountVO jztCodeCirculationListCountVO=new JztCodeCirculationListCountVO();
            jztCodeCirculationListCountVO.setJztCodeCirculationListCount(count);
            return  DtoUtil.returnFail("jztCodeCirculation count:"+count,"100002",jztCodeCirculationListCountVO);
        }

        if(count>0){
            JztCodeCirculationListCountVO jztCodeCirculationListCountVO=new JztCodeCirculationListCountVO();
            jztCodeCirculationListCountVO.setJztCodeCirculationListCount(count);
            return  DtoUtil.returnSuccess("jztCodeCirculation count:"+count,jztCodeCirculationListCountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }


    }

    @RequestMapping(value = "/selectJztCodeCirculationAttachmentCountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectJztCodeCirculationAttachmentCountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataType="jztCodeCirculationAttachment";
        double count=0.00;
        try {
            count=hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataType);
        }catch (Exception e){
            JztCodeCirculationAttachmentFileSizeCountVO jztCodeCirculationAttachmentFileSizeCountVO=new JztCodeCirculationAttachmentFileSizeCountVO();
            jztCodeCirculationAttachmentFileSizeCountVO.setJztCodeCirculationAttachmentFileSizeCount(count);
            return  DtoUtil.returnFail("jztCodeCirculationAttachment file size count:"+count+"MB","100002",jztCodeCirculationAttachmentFileSizeCountVO);
        }

        if(count>0){
            JztCodeCirculationAttachmentFileSizeCountVO jztCodeCirculationAttachmentFileSizeCountVO=new JztCodeCirculationAttachmentFileSizeCountVO();
            jztCodeCirculationAttachmentFileSizeCountVO.setJztCodeCirculationAttachmentFileSizeCount(count);
            return  DtoUtil.returnSuccess("jztCodeCirculationAttachment file size count:"+count+"MB",jztCodeCirculationAttachmentFileSizeCountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }


    }


    @RequestMapping(value = "/selectJztGS1CountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectJztGS1CountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataType="jztGS1List";
        Integer count=0;
        try {
            count=(int)hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataType);
        }catch (Exception e){
            JztGS1CountVO jztGS1CountVO=new JztGS1CountVO();
            jztGS1CountVO.setJztGS1Count(count);
            return  DtoUtil.returnFail("jztGS1 count:"+count,"100002",jztGS1CountVO);
        }

        if(count>0){
            JztGS1CountVO jztGS1CountVO=new JztGS1CountVO();
            jztGS1CountVO.setJztGS1Count(count);
            return  DtoUtil.returnSuccess("jztGS1 count:"+count,jztGS1CountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }


    }

    @RequestMapping(value = "/selectGS1AttachmentCountByUserName", method = RequestMethod.POST)
    @ResponseBody
    public Dto selectGS1AttachmentCountByUserName(@RequestParam(value = "userName", required = true)String userName) throws Exception {

        String dataType="jztGS1Attachment";
        double count=0.00;
        try {
            count=hashChainStatisticsService.selectCountByUserNameAndDataType(userName,dataType);
        }catch (Exception e){
           JztGS1AttachmentFileSizeCountVO jztGS1AttachmentFileSizeCountVO=new JztGS1AttachmentFileSizeCountVO();
            jztGS1AttachmentFileSizeCountVO.setJztGS1AttachmentFileSizeCount(count);
            return  DtoUtil.returnFail("jztGS1Attachment file size count:"+count+"MB","100002",jztGS1AttachmentFileSizeCountVO);
        }

        if(count>0){
            JztGS1AttachmentFileSizeCountVO jztGS1AttachmentFileSizeCountVO=new JztGS1AttachmentFileSizeCountVO();
            jztGS1AttachmentFileSizeCountVO.setJztGS1AttachmentFileSizeCount(count);
            return  DtoUtil.returnSuccess("jztGS1Attachment file size count:"+count+"MB",jztGS1AttachmentFileSizeCountVO);
        }else{
            return DtoUtil.returnFail("No count","100001");
        }


    }


}
